package com.xin.ruanjian.omsystem.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Base64;

import com.xin.ruanjian.omsystem.bases.Login;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * Created by local123 on 2017/12/28.
 */

public class MyApplication extends MultiDexApplication {
    private static MyApplication instance;
    private static Context context;
    private static Handler handler;
    public static Thread mainThread;   //主线程
    public static int mainThreadId;    //主线程id
    private static int screenWidth, screenHeight;
    private static float density;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        instance = this;
        handler = new Handler();
        mainThread = Thread.currentThread();//当前实例化MyApplication的线程，就是主线程
        mainThreadId = android.os.Process.myTid();//获取当前线程的id

        //获取当前屏幕宽高度以及屏幕密度
        screenWidth = getResources().getDisplayMetrics().widthPixels;
        screenHeight = getResources().getDisplayMetrics().heightPixels;
        density = getResources().getDisplayMetrics().density;

        //初始化数据库
        Realm.init(context);
        RealmConfiguration config = new  RealmConfiguration.Builder()
                .name("ShopCarData.realm")
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(config);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    public static MyApplication getInstance() {
        return instance;
    }

    public static Handler getHandler() {
        return MyApplication.handler;
    }

    public static Context getContext() {
        return context;
    }

    public static int getScreenWidth() {
        return screenWidth;
    }

    public static int getScreenHeight() {
        return screenHeight;
    }

    public static float getDensity() {
        return density;
    }

    /**
     * 保存用户登录信息
     */
    public static boolean saveLogin(Login login) {
        SharedPreferences sp = context.getSharedPreferences("OMSystem", Context.MODE_PRIVATE);
        if (login == null) {
            SharedPreferences.Editor editor = sp.edit();
            return editor.clear().commit();
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(baos);
            out.writeObject(login);
            String loginVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("LoginMessage", loginVal);
            return editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    baos.close();
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return false;
    }

    /**
     * 获取用户登录信息
     */
    public static Login getLogin() {
        SharedPreferences sp = context.getSharedPreferences("OMSystem", Context.MODE_PRIVATE);
        if (sp.contains("LoginMessage")) {
            String loginVal = sp.getString("LoginMessage", null);
            byte[] buffer = Base64.decode(loginVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                return (Login) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    bais.close();
                    if (ois != null) ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


}
